import axios from 'axios'
import store from '@/store'
import notification from 'ant-design-vue/es/notification'
import storage from 'store'

// 创建 axios 实例
const request = axios.create({
})

request.interceptors.request.use(config => {
  const token = storage.get('USERINFO')?.token

  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token
  }
  return config
})

request.interceptors.response.use((response) => {
  if (response.data.retCode === 10006) {
    notification.error({
      message: '错误',
      description: '用户登录过期'
    })
    const token = storage.get('USERINFO')?.token
    if (token) {
      store.dispatch('Logout')
        .then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 0)
        })
    }
  }
  return response.data
})

export default request
